package framework.spatial;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

/**
 *
 * @author Lukáš Marek
 */
public class FRDSpatialMapImageBackground implements FRDSpatialMapBackground
{
	int width;
	int height;
	
	double zoom;
	double maxScale;
	double minScale;
	double scaleStep;
	Point zoomLocation;
	
	BufferedImage image = null;
	
	public FRDSpatialMapImageBackground(URL resource)
	{
		try 
		{
			image = ImageIO.read(resource);
		}
		catch (IOException e)	{ System.err.println(e.getMessage()); }
		zoom = 1.0;
		maxScale = 4.0;
		minScale = 1.0;
		scaleStep = 0.5;
		zoomLocation = new Point(0,0);
	}

	@Override
	public void paint(Graphics g)
	{
		g.drawImage(image, 0, 0, width, height,
								zoomLocation.x,
								zoomLocation.y,
								zoomLocation.x + (int)(image.getWidth()/zoom),
								zoomLocation.y + (int)(image.getHeight()/zoom),
								null);
	}
	
	@Override
	public void setPosition(Point p)
	{
		Point zl = new Point(p);
		if (zl.x < 0) zl.x = 0;
		if (zl.y < 0) zl.y = 0;
		if (zl.x > image.getWidth() - (int)(image.getWidth()/zoom)) zl.x = image.getWidth() - (int)(image.getWidth()/zoom);
		if (zl.y > image.getHeight() - (int)(image.getHeight()/zoom)) zl.y = image.getHeight() - (int)(image.getHeight()/zoom);
		zoomLocation.setLocation(zl);
	}
	
	@Override
	public Point getPosition()
	{
		return zoomLocation;
	}
	
	@Override
	public void setHeight(int height)
	{
		this.height = height;
	}

	@Override
	public void setWidth(int width)
	{
		this.width = width;
	}

	@Override
	public void setDefaultZoom(double zoom)
	{
		this.zoom = zoom;
	}

	@Override
	public void setMaxZoom(double maxScale)
	{
		this.maxScale = maxScale;
	}

	@Override
	public void setMinZoom(double minScale)
	{
		this.minScale = minScale;
	}

	@Override
	public void setZoomStep(double step)
	{
		this.scaleStep = step;
	}

	@Override
	public void zoomOut()
	{
		Point oldSize = new Point((int)(image.getWidth()/zoom), (int)(image.getHeight()/zoom));
		zoom = Math.max(zoom - scaleStep, minScale);
		Point newSize = new Point((int)(image.getWidth()/zoom), (int)(image.getHeight()/zoom));
		Point p = new Point(zoomLocation);
		if (oldSize.x != newSize.x)
		{
			p.x -= (newSize.x - oldSize.x)/2;
			p.y -= (newSize.y - oldSize.y)/2;
		}
		this.setPosition(p);
	}

	@Override
	public void zoomIn()
	{
		Point oldSize = new Point((int)(image.getWidth()/zoom), (int)(image.getHeight()/zoom));
		zoom = Math.min(zoom + scaleStep, maxScale);
		Point newSize = new Point((int)(image.getWidth()/zoom), (int)(image.getHeight()/zoom));
		Point p = new Point(zoomLocation);
		if (oldSize.x != newSize.x)
		{
			p.x += (oldSize.x - newSize.x)/2;
			p.y += (oldSize.y - newSize.y)/2;
		}
		this.setPosition(p);
	}

	@Override
	public double getZoom()
	{
		return zoom;
	}
	
	@Override
	public double getScale()
	{
		return image.getWidth() / (double)width;
	}
	
	/**
	 * vrati velikost mapy
	 * @return 
	 */
	public Point mapSize()
	{
		return new Point(image.getWidth(), image.getHeight());
	}
}
